package org.velocity;

import net.e175.klaus.solarpositioning.AzimuthZenithAngle;
import net.e175.klaus.solarpositioning.DeltaT;
import net.e175.klaus.solarpositioning.SPA;
import net.e175.klaus.solarpositioning.SunriseTransitSet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * ClassName: Finally
 * Package: org.example
 * Description:
 *
 * @Author BuTang
 * @Create 2023/11/22 15:44
 * @Version 1.0
 */
public class Finally {

    /**
     * 把一天的太阳高度角写入到已有的文件
     * @throws Exception
     */
    public void test1() throws Exception{

        long start = System.currentTimeMillis();

        //计算太阳高度角的参数，从上到下分别为纬度，经度，海拔高度，大气压力，温度，时区（zoneIdName，zoneId）
        double latitude = 30.23800;
        double longitude = 120.13431;
        double elevation = 10;
        double pressure = 1000;
        double temperature = 5;
        String zoneIdName = "Asia/Shanghai";
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        //创建需要时间节点
        LocalDateTime dateTime = LocalDateTime.of(2023, 11, 20, 10, 30, 0);
        double deltaT = DeltaT.estimate(LocalDate.from(dateTime));
        ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime,zoneId);
        SunriseTransitSet sunriseTransitSet = SPA.calculateSunriseTransitSet(zonedDateTime, latitude, longitude, deltaT);
        ZonedDateTime sunrise = sunriseTransitSet.getSunrise();
        ZonedDateTime sunset = sunriseTransitSet.getSunset();

        String filePath = "C:\\Users\\11921\\Desktop\\Java-Working\\my_project\\AngularVelocity\\src\\main\\java\\org\\example\\velocity.xlsx";
        FileInputStream fileInputStream = new FileInputStream(filePath) ;
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheet("sheet1");




        //开始计算，获取结果result
        AzimuthZenithAngle result =SpaUtils.getAzimuthAndZenithAngle(sunrise.toLocalDateTime(), zoneId, latitude, longitude, elevation, pressure, temperature);

        //高度角
        double eleAngle = 90 - result.getZenithAngle();
        System.out.println("此时太阳高度角为："+eleAngle+"°");
        //方位角
        double aziumthAngel = result.getAzimuth();
        System.out.println("此时太阳方位角为："+aziumthAngel+"°");
        System.out.println("计算 "+ dateTime+ " 的太阳位置总耗时为 " +(System.currentTimeMillis()-start) +"ms");


        LocalDateTime oldTime = dateTime ;
        LocalDateTime nowTime = dateTime ;

        //高度角
        double oldAngle1 ;
        double nowAngle1 = 0;
        //方位角
        double oldAngle2 ;
        double nowAngle2 = 0;
        //高度角
        double anteredAngle1 ;
        //方位角
        double anteredAngle2 ;
        double num = 1;

//        //用于高度角的累计
//        double addNumber1 = 0 ;
//        double addNumber2 = 0 ;

        oldAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();

        oldAngle2 = SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();
        int i =1 ;
        while (nowTime.compareTo(sunset.toLocalDateTime()) < 0){


            //获取行
            Row row = sheet.createRow(i);


            nowTime = nowTime.plusSeconds(1);

            //高度角有关的计算
            nowAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();
            anteredAngle1 = Math.abs( Math.abs(nowAngle1)  - Math.abs(oldAngle1) )  ;
            row.createCell(1).setCellValue(anteredAngle1);
//            addNumber1 += anteredAngle1;

            //方位角 角有关的计算
            nowAngle2 = SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();
            anteredAngle2 = Math.abs( Math.abs(nowAngle2)  - Math.abs(oldAngle2) ) ;
            row.createCell(2).setCellValue(anteredAngle2);
//            addNumber2 += anteredAngle2;

            oldAngle1 = nowAngle1;
            oldAngle2 = nowAngle2;
            i++;
        }

        // 保存文件
        FileOutputStream outputStream = new FileOutputStream(filePath) ;
        workbook.write(outputStream);
         System.out.println("Excel文件已成功修改！");

    }

    /**
     * 写出到指定目录（一天内的太阳高度角）
     * @throws Exception
     */
    public void test2() throws Exception{

        long start = System.currentTimeMillis();

        //计算太阳高度角的参数，从上到下分别为纬度，经度，海拔高度，大气压力，温度，时区（zoneIdName，zoneId）
        double latitude = 30.23800;
        double longitude = 120.13431;
        double elevation = 10;
        double pressure = 1000;
        double temperature = 5;
        String zoneIdName = "Asia/Shanghai";
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        //创建需要时间节点
        LocalDateTime dateTime = LocalDateTime.of(2023, 11, 20, 10, 30, 0);
        double deltaT = DeltaT.estimate(LocalDate.from(dateTime));
        ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime,zoneId);
        SunriseTransitSet sunriseTransitSet = SPA.calculateSunriseTransitSet(zonedDateTime, latitude, longitude, deltaT);
        ZonedDateTime sunrise = sunriseTransitSet.getSunrise();
        ZonedDateTime sunset = sunriseTransitSet.getSunset();

        String filePath = "src/main/java/org/example/file/month1.xlsx";

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet1");

        //开始计算，获取结果result
        AzimuthZenithAngle result =SpaUtils.getAzimuthAndZenithAngle(sunrise.toLocalDateTime(), zoneId, latitude, longitude, elevation, pressure, temperature);

        //高度角
        double eleAngle = 90 - result.getZenithAngle();
        System.out.println("此时太阳高度角为："+eleAngle+"°");
        //方位角
        double aziumthAngel = result.getAzimuth();
        System.out.println("此时太阳方位角为："+aziumthAngel+"°");
        System.out.println("计算 "+ dateTime+ " 的太阳位置总耗时为 " +(System.currentTimeMillis()-start) +"ms");


        LocalDateTime oldTime = dateTime ;
        LocalDateTime nowTime = dateTime ;

        //高度角
        double oldAngle1 ;
        double nowAngle1 = 0;
        //方位角
        double oldAngle2 ;
        double nowAngle2 = 0;
        //高度角
        double anteredAngle1 ;
        //方位角
        double anteredAngle2 ;
        double num = 1;

//        //用于高度角的累计
//        double addNumber1 = 0 ;
//        double addNumber2 = 0 ;

        oldAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();

        oldAngle2 = SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();
        int i =1 ;
        while (nowTime.compareTo(sunset.toLocalDateTime()) < 0){


            //创建行
            Row row = sheet.createRow(i);


            nowTime = nowTime.plusSeconds(1);

            //高度角有关的计算
            nowAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();
            anteredAngle1 = Math.abs( Math.abs(nowAngle1)  - Math.abs(oldAngle1) )  ;
            row.createCell(1).setCellValue(anteredAngle1);
//            addNumber1 += anteredAngle1;

            //方位角 角有关的计算
            nowAngle2 = SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();
            anteredAngle2 = Math.abs( Math.abs(nowAngle2)  - Math.abs(oldAngle2) ) ;
            row.createCell(2).setCellValue(anteredAngle2);
//            addNumber2 += anteredAngle2;

            oldAngle1 = nowAngle1;
            oldAngle2 = nowAngle2;
            i++;
        }

        // 保存文件
        FileOutputStream outputStream = new FileOutputStream(filePath) ;
        workbook.write(outputStream);
        System.out.println("Excel文件已成功修改！");

    }

    /**
     * 创建文件，计算n天的太阳高度角
     * @throws Exception
     */
    @Test
    public void cacuDaysVelocity() throws Exception{

        long start = System.currentTimeMillis();

        //计算太阳高度角的参数，从上到下分别为纬度，经度，海拔高度，大气压力，温度，时区（zoneIdName，zoneId），需要计算的天数
        double latitude = 30.23800;
        double longitude = 120.13431;
        double elevation = 10;
        double pressure = 1000;
        double temperature = 5;
        String zoneIdName = "Asia/Shanghai";
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        int calcDays = 365 ;

        //创建需要时间节点
        LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 0, 0, 0);
        double deltaT = DeltaT.estimate(LocalDate.from(dateTime));
        ZonedDateTime time  = ZonedDateTime.of(dateTime, zoneId);
        ZonedDateTime zonedDateTime  = ZonedDateTime.of(dateTime,zoneId);

        //文件存放路径
        String pre =  "C:\\Users\\11921\\Desktop\\Java-Working\\my_project\\file_value\\file\\day";
        String xlsx = ".xlsx" ;

        //用于记录那一天的值
        int day = 1 ;
        //用于记录文件的行数
        int i =1 ;
        int number = 0;

//                ZonedDateTime zonedDateTime = time.plusDays(number);

            //计算365天
            while (day < calcDays ){
                zonedDateTime.plusDays(number);
                System.out.println("开始计算一年中的第 ： "+zonedDateTime.getDayOfYear() + " 天");
                Workbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet("sheet1");
                Row row1 = sheet.createRow(0);
                row1.createCell(0).setCellValue("序号");
                row1.createCell(1).setCellValue("太阳高度角角速度( 单位：°/s)");
                row1.createCell(2).setCellValue("太阳方位角角速度( 单位：°/s)");
                String filePath = pre + day + xlsx;
                i++;

            SunriseTransitSet sunriseTransitSet = SPA.calculateSunriseTransitSet(zonedDateTime, latitude, longitude, deltaT);
            ZonedDateTime sunrise = sunriseTransitSet.getSunrise();
            ZonedDateTime sunset = sunriseTransitSet.getSunset();

            //高度角
            LocalDateTime oldTime = sunrise.toLocalDateTime() ;
            LocalDateTime nowTime = sunrise.toLocalDateTime() ;

            //高度角
            double oldAngle1 ;
            double nowAngle1 = 0;
            //方位角
            double oldAngle2 ;
            double nowAngle2 = 0;
            //高度角
            double anteredAngle1 ;
            //方位角
            double anteredAngle2 ;
            double num = 1;

            oldAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();

            oldAngle2 = SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();

            //计算24小时内每秒
            while (nowTime.compareTo(sunset.toLocalDateTime()) < 0){

                //创建行
                Row row = sheet.createRow(i);
                nowTime = nowTime.plusSeconds(1);

                //row.createCell(0).setCellValue(LocalDateTime.now().toString());
                row.createCell(0).setCellValue(nowTime.toString());

                //高度角有关的计算
                nowAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();

                //速度绝对值
                //anteredAngle1 = Math.abs( Math.abs(nowAngle1)  - Math.abs(oldAngle1) )  ;
                anteredAngle1 = nowAngle1  - oldAngle1  ;

                row.createCell(1).setCellValue(anteredAngle1);
//            addNumber1 += anteredAngle1;

                //方位角 角有关的计算
                nowAngle2 = SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();

                //速度绝对值
//                anteredAngle2 = Math.abs( Math.abs(nowAngle2)  - Math.abs(oldAngle2) ) ;
                anteredAngle2 = nowAngle2  - oldAngle2 ;

                row.createCell(2).setCellValue(anteredAngle2);
//            addNumber2 += anteredAngle2;

                oldAngle1 = nowAngle1;
                oldAngle2 = nowAngle2;
                i++;
//                nowTime.plusSeconds(1);

            }
                // 保存文件
                FileOutputStream outputStream = new FileOutputStream(filePath) ;
                workbook.write(outputStream);
                System.out.println(day + "计算结束，输出。");
                day ++ ;
                i = 0;
                number ++ ;
                zonedDateTime = time.plusDays(number);

            }

//        }
    }
    }

